package com.base.dehong.util;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;

/**
 * 
 * @author TangWenhan/YeFengyun
 *2015-11-20下午1:50:26
 */
public class PhotoUtil {
//	private String mBaseFilePath = Environment.getExternalStorageDirectory().toString();
	private static String mFileName = "", out_file_path = "";
	private String tempFilePath = "";
	private Activity mActivity;	
	
	public PhotoUtil(Activity activity) {
		// 判断SD卡是否存在并具有读写权限
		if (CommonUtil.sdCardIsAvailable()) {
			out_file_path = Environment.getExternalStorageDirectory()
					+ "/dehong/pictures/";
			File dir = new File(out_file_path);
			if (!dir.exists()) {
				dir.mkdirs();
			}
		}
		mActivity = activity;
	}
	public  void takePhotoByCamera(Uri imageUri, int REQUEST){
		Intent cameraintent = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        // 指定调用相机拍照后照片的储存路径
        cameraintent.putExtra(MediaStore.EXTRA_OUTPUT,
                imageUri);
        mActivity.startActivityForResult(cameraintent,
        		REQUEST);
	}
	public void takePhotoByAlbum(int REQUEST){
		Intent getAlbum = new Intent(Intent.ACTION_PICK, 
				android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        getAlbum.setType("image/*");
        mActivity.startActivityForResult(getAlbum, REQUEST);
	}
	public void startPhotoZoom(int x ,int y,Uri uri,int REQUEST) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", x);
        intent.putExtra("outputY", y);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
		intent.putExtra("return-data", false);
		intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
		intent.putExtra("noFaceDetection", true); // no face detection
        mActivity.startActivityForResult(intent, REQUEST);
    }
	
	public void startPhotoZoom(int size ,Uri uri, int REQUEST) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        // crop为true是设置在开启的intent中设置显示的view可以剪裁
        intent.putExtra("crop", "true");

        // aspectX aspectY 是宽高的比例
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);

        // outputX,outputY 是剪裁图片的宽高
        intent.putExtra("outputX", size);
        intent.putExtra("outputY", size);
        intent.putExtra("return-data", true);
        intent.putExtra("noFaceDetection", true);
        System.out.println("22================");
        mActivity.startActivityForResult(intent, REQUEST);
    }
	
	public void startPhotoZoom(Uri uri,int REQUEST){
		startPhotoZoom(200,200,uri,REQUEST);
	}
	
	public String getFile(){
		tempFilePath = out_file_path + mFileName;
		return tempFilePath;
	}

	public void setFileName(String path){
		mFileName = path;
	}
}

